package com.hyg.lib_common.DataUtil;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.alibaba.fastjson.JSONObject;
import com.hyg.lib_common.DataModel.personal.UserDetail;
import com.hyg.lib_common.constant.FileConstants;

/**
 * description 用户本地存储信息
 *
 * @author: Administrator
 * @date: 2020/12/7
 */
public class UserSPUtils {

    public static SharedPreferences getUserInfoSp(Context context) {
        return context.getSharedPreferences(FileConstants.UserInfo, Context.MODE_PRIVATE);
    }

    public static SharedPreferences getSystemSettingSp(Context context) {
        return context.getSharedPreferences(FileConstants.SystemSettings, Context.MODE_PRIVATE);
    }

    public static SharedPreferences getMusicSettingSp(Context context) {
        return context.getSharedPreferences(FileConstants.MusicSettings, Context.MODE_PRIVATE);
    }


    /**
     * description: 获取详细信息
     *
     * @author visitant
     * @time 2020/12/8 13:15
     */
    public static UserDetail getUserDetail(Context context) {
        UserDetail userDetail = JSONObject.parseObject(getUserInfoSp(context).getString(FileConstants.UserDetailData, ""), UserDetail.class);
        return userDetail != null ? userDetail : new UserDetail();
    }

    /**
     * description: 获取手机号
     *
     * @author visitant
     * @time 2020/12/8 13:14
     */
    public static String getUserPhone(Context context) {
        return getUserInfoSp(context).getString(FileConstants.UserPhone, "");
    }

    /**
     * description: 获取Token
     *
     * @author visitant
     * @time 2020/12/8 13:15
     */
    public static String getToken(Context context) {
        return getUserInfoSp(context).getString(FileConstants.Token, "");
    }

    /**
     * description:   获取密码
     *
     * @author visitant
     * @time 2020/12/8 13:15
     */
    public static String getUserPass(Context context) {
        return getUserInfoSp(context).getString(FileConstants.UserPassword, "");
    }

    public static void clearUserInfo(Context context) {
        SharedPreferences.Editor editor = getUserInfoSp(context).edit();
        editor.clear();
        editor.apply();
    }

    public static void putUserString(Context context, String key, String value) {
        SharedPreferences.Editor editor = getUserInfoSp(context).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getUserString(Context context, String key) {
        return getUserInfoSp(context).getString(key, "");
    }


    public static String getUserVersionSetting(Context context) {
        return getSystemSettingSp(context).getString(FileConstants.IsSkip, "false");
    }

    public static String getUserVersionCode(Context context) {
        return getSystemSettingSp(context).getString(FileConstants.VersionCode, "1.0");
    }

    public static void putVersionSetting(Context context, String IsSkip, String VersionCode) {
        SharedPreferences.Editor editor = getSystemSettingSp(context).edit();
        editor.putString(FileConstants.IsSkip, IsSkip);
        editor.putString(FileConstants.VersionCode, VersionCode);
        editor.apply();
    }


    // 获取是否允许4G
    public static boolean getAllowMobile(Context mContext) {

        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        return sp.getBoolean("play_mobile", false);
    }

    // 设置是否允许4G
    public static void setAllowMobile(Context mContext, boolean isAllow) {
        SharedPreferences sp = PreferenceManager
                .getDefaultSharedPreferences(mContext);
        SharedPreferences.Editor editor = sp.edit();
        editor.putBoolean("play_mobile", isAllow);
        editor.commit();
    }


    //清除养生建议缓存信息
    public static void clearHealthAdviceInfo(Context context) {
        SharedPreferences sharedPreferences = UserSPUtils.getUserInfoSp(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(FileConstants.Zuyu_data, "");
        editor.putString(FileConstants.XieZi, "");
        editor.putString(FileConstants.YunDong_data, "");
        editor.putString(FileConstants.YingJi, "");
        editor.putString(FileConstants.Yinshi_data, "");
        editor.putString(FileConstants.Liliao_data, "");
        editor.putString(FileConstants.ChaYin, "");
        editor.apply();
    }

    public static void clearMusicSetting(Context context) {
        SharedPreferences sharedPreferences = UserSPUtils.getMusicSettingSp(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(FileConstants.ClockStartTime, 0);
        editor.putLong(FileConstants.ClockMin, 0);
        editor.apply();
    }

    public static long getMusicClockStartTime(Context context) {
        SharedPreferences sharedPreferences = UserSPUtils.getMusicSettingSp(context);
        return sharedPreferences.getLong(FileConstants.ClockStartTime, 0);
    }

    public static long getMusicClockSettingMin(Context context) {
        SharedPreferences sharedPreferences = UserSPUtils.getMusicSettingSp(context);
        return sharedPreferences.getLong(FileConstants.ClockMin, 0);
    }

    public static void setMusicSetting(Context context,long startTime,long min) {
        SharedPreferences sharedPreferences = UserSPUtils.getMusicSettingSp(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(FileConstants.ClockStartTime, startTime);
        editor.putLong(FileConstants.ClockMin, min);
        editor.apply();
    }



}
